package com.ruoyi.lm.controller;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.lm.domain.LmSerinfo;
import com.ruoyi.lm.service.ILmSerinfoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.lm.domain.LmSerinfotype;
import com.ruoyi.lm.service.ILmSerinfotypeService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 服务分类Controller
 * 
 * @author ruoyi
 * @date 2020-09-02
 */
@Controller
@RequestMapping("/lm/serinfotype")
public class LmSerinfotypeController extends BaseController
{
    private String prefix = "lm/serinfotype";

    @Autowired
    private ILmSerinfotypeService lmSerinfotypeService;
    @Autowired
    private ILmSerinfoService lmSerinfoService;

    @RequiresPermissions("lm:serinfotype:view")
    @GetMapping()
    public String serinfotype()
    {
        return prefix + "/serinfotype";
    }

    /**
     * 表格回传父窗体
     */
    @GetMapping("/parent")
    public String parent()
    {
        return prefix + "/parent";
    }

    /**
     * 查询服务分类列表
     */
    @RequiresPermissions("lm:serinfotype:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(LmSerinfotype lmSerinfotype)
    {
        startPage();
        List<LmSerinfotype> resultList = new ArrayList<>();
        lmSerinfotype.setLevel("0");
        List<LmSerinfotype> list = lmSerinfotypeService.selectLmSerinfotypeList(lmSerinfotype);
        for(LmSerinfotype temp :list){
            //获取所有二级分类
            LmSerinfotype childTemp = new LmSerinfotype();
            childTemp.setFid(temp.getId());
            List<LmSerinfotype> childList = lmSerinfotypeService.selectLmSerinfotypeList(childTemp);
            int sum =0;
            for(int i=0;i<childList.size();i++){
                LmSerinfo lmSerinfo = new LmSerinfo();
                lmSerinfo.setServiceType(childList.get(i).getId());
                sum +=lmSerinfoService.selectLmSerinfoList(lmSerinfo).size();
            }
            temp.setServiceCount(sum);
        }
        return getDataTable(list);
    }

    /**
     * 查询服务分类列表
     */
    @RequiresPermissions("lm:serinfotype:list")
    @PostMapping("/listChild")
    @ResponseBody
    public TableDataInfo listChild(String id)
    {
        startPage();
        LmSerinfotype lmSerinfotype = new LmSerinfotype();
        lmSerinfotype.setFid(id);
        List<LmSerinfotype> list = lmSerinfotypeService.selectLmSerinfotypeList(lmSerinfotype);
        for(LmSerinfotype temp : list){
                LmSerinfo lmSerinfo = new LmSerinfo();
                lmSerinfo.setServiceType(temp.getId());
                temp.setServiceCount(lmSerinfoService.selectLmSerinfoList(lmSerinfo).size());
        }
        return getDataTable(list);
    }

    /**
     * 查询一级服务分类列表
     */
    @RequiresPermissions("lm:serinfotype:list")
    @PostMapping("/listFirstLevel")
    @ResponseBody
    public TableDataInfo listFirstLevel(LmSerinfotype lmSerinfotype)
    {
        startPage();
        lmSerinfotype.setLevel("0");
        List<LmSerinfotype> list = lmSerinfotypeService.selectLmSerinfotypeList(lmSerinfotype);
        return getDataTable(list);
    }

    /**
     * 导出服务分类列表
     */
    @RequiresPermissions("lm:serinfotype:export")
    @Log(title = "服务分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(LmSerinfotype lmSerinfotype)
    {
        List<LmSerinfotype> list = lmSerinfotypeService.selectLmSerinfotypeList(lmSerinfotype);
        ExcelUtil<LmSerinfotype> util = new ExcelUtil<LmSerinfotype>(LmSerinfotype.class);
        return util.exportExcel(list, "serinfotype");
    }

    /**
     * 新增服务分类
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存服务分类
     */
    @RequiresPermissions("lm:serinfotype:add")
    @Log(title = "服务分类", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(LmSerinfotype lmSerinfotype)
    {
        if(lmSerinfotype.getFid() == null || "".equals(lmSerinfotype.getFid())){
            lmSerinfotype.setFid("0");
        }
        return toAjax(lmSerinfotypeService.insertLmSerinfotype(lmSerinfotype));
    }

    /**
     * 修改服务分类
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        LmSerinfotype lmSerinfotype = lmSerinfotypeService.selectLmSerinfotypeById(id);
        mmap.put("lmSerinfotype", lmSerinfotype);
        return prefix + "/edit";
    }

    /**
     * 修改保存服务分类
     */
    @RequiresPermissions("lm:serinfotype:edit")
    @Log(title = "服务分类", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(LmSerinfotype lmSerinfotype)
    {
        return toAjax(lmSerinfotypeService.updateLmSerinfotype(lmSerinfotype));
    }

    /**
     * 删除服务分类
     */
    @RequiresPermissions("lm:serinfotype:remove")
    @Log(title = "服务分类", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(lmSerinfotypeService.deleteLmSerinfotypeByIds(ids));
    }
}
